package com.clientes;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.util.Log;

public class RatingBean{
	private int IdCliente;
	private int IdEvento;
	private int Rating;
	int TIMEOUT_MILLISEC = 10000; 
	
	public int getIdCliente() {
		return IdCliente;
	}
	public void setIdCliente(int idCliente) {
		IdCliente = idCliente;
	}
	public int getIdEvento() {
		return IdEvento;
	}
	public void setIdEvento(int idEvento) {
		IdEvento = idEvento;
	}
	public int getRating() {
		return Rating;
	}
	public void setRating(int rating) {
		Rating = rating;
	}
	
	public RatingBean(){
		super();
	}
	public RatingBean(int idCliente, int idEvento) {
		super();
		IdCliente = idCliente;
		IdEvento = idEvento;
	}
	
	
	public boolean RegistrarRating(int rating){
		
		boolean respuesta=false;
			try {
				Log.i("webservice","registrarrating");
				//StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

		       	  //StrictMode.setThreadPolicy(policy); 
				
				
				HttpParams httpParams = new BasicHttpParams();
				HttpConnectionParams.setConnectionTimeout(httpParams,TIMEOUT_MILLISEC);
				HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
				HttpClient client = new DefaultHttpClient(httpParams);

				Log.i("webservice","2");
				String url = "http://200.16.7.112:3000/event_management/events/actualizar_rating/";

				
				JSONObject json = new JSONObject();
				

				Log.i("webservice","idc: "+IdCliente+ "   ideve:"+IdEvento);
				
				HttpPost httppost  = new HttpPost(url);
				   httppost.setHeader("Content-type", "application/json");
				   
				   json.put("nuevo_rating", rating);
					json.put("id_event", IdEvento);
					json.put("user_id", IdCliente);
					Log.i("string", json.toString());
					
					StringEntity se = new StringEntity(json.toString()); 
			       se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
			        httppost.setEntity(se); 
			        Log.i("MMM", "antes de execute");
			        HttpResponse response = client.execute(httppost);
			        Log.i("MMM", "despues de execute");
			        String temp = EntityUtils.toString(response.getEntity());
			        Log.i("DESPUES", "despues de temp");
			        //Log.i("tag", temp);
			        respuesta=true;
			        Log.i("fin de WS", "termino");
				// If the response does not enclose an entity, there is no need
			} catch (Throwable t) {
				Log.i("fallo","fallo");
				return respuesta;
			}
		
			
		return respuesta;
	}

	
}